<!DOCTYPE html>
<html >

<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>《属性数据分析》代码</title>
  <meta name="description" content="《属性数据分析》代码。">
  <meta name="generator" content="bookdown 0.7 and GitBook 2.6.7">

  <meta property="og:title" content="《属性数据分析》代码" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="《属性数据分析》代码。" />
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="《属性数据分析》代码" />
  
  <meta name="twitter:description" content="《属性数据分析》代码。" />
  



<meta name="date" content="2018-09-05">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  
  
<link rel="prev" href="intro.html">
<link rel="next" href="glm.html">
<script src="libs/jquery/jquery.min.js"></script>
<link href="libs/gitbook/css/style.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-fontsettings.css" rel="stylesheet" />









<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(data-line-number);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./index.html">《属性数据分析》代码</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>前言</a></li>
<li class="chapter" data-level="1" data-path="intro.html"><a href="intro.html"><i class="fa fa-check"></i><b>1</b> 导言</a><ul>
<li class="chapter" data-level="1.1" data-path="intro.html"><a href="intro.html#data-intro"><i class="fa fa-check"></i><b>1.1</b> 属性响应数据</a></li>
<li class="chapter" data-level="1.2" data-path="intro.html"><a href="intro.html#prob-dist"><i class="fa fa-check"></i><b>1.2</b> 属性数据的概率分布</a><ul>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#二项分布计算"><i class="fa fa-check"></i>二项分布计算</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="intro.html"><a href="intro.html#stat-infer"><i class="fa fa-check"></i><b>1.3</b> 比例的统计推断</a><ul>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#二项分布似然函数图"><i class="fa fa-check"></i>二项分布似然函数图</a></li>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#二项分布假设检验"><i class="fa fa-check"></i>二项分布假设检验</a></li>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#二项分布置信区间"><i class="fa fa-check"></i>二项分布置信区间</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="intro.html"><a href="intro.html#more-stat-infer"><i class="fa fa-check"></i><b>1.4</b> 关于离散数据的更多统计推断</a><ul>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#二项分布参数统计推断"><i class="fa fa-check"></i>二项分布参数统计推断</a></li>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#小样本推断"><i class="fa fa-check"></i>小样本推断</a></li>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#小样本推断p值调整"><i class="fa fa-check"></i>小样本推断P值调整</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#problems-ch1"><i class="fa fa-check"></i>课后题</a><ul>
<li class="chapter" data-level="" data-path="intro.html"><a href="intro.html#第4题"><i class="fa fa-check"></i>第4题</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="contingency-table.html"><a href="contingency-table.html"><i class="fa fa-check"></i><b>2</b> 列联表</a><ul>
<li class="chapter" data-level="2.1" data-path="contingency-table.html"><a href="contingency-table.html#stucture"><i class="fa fa-check"></i><b>2.1</b> 列联表的概率结构</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#关于来世"><i class="fa fa-check"></i>关于来世</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="contingency-table.html"><a href="contingency-table.html#prop-compare"><i class="fa fa-check"></i><b>2.2</b> 2×2表比例的比较</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#阿司匹林与心脏病列联表检验"><i class="fa fa-check"></i>阿司匹林与心脏病（列联表检验）</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="contingency-table.html"><a href="contingency-table.html#odds-ratio"><i class="fa fa-check"></i><b>2.3</b> 优势比</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#阿司匹林与心脏病优势比"><i class="fa fa-check"></i>阿司匹林与心脏病（优势比）</a></li>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#吸烟状态与心肌梗死"><i class="fa fa-check"></i>吸烟状态与心肌梗死</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="contingency-table.html"><a href="contingency-table.html#chi-square-test"><i class="fa fa-check"></i><b>2.4</b> 独立性的卡方检验</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#性别和党派认同"><i class="fa fa-check"></i>性别和党派认同</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="contingency-table.html"><a href="contingency-table.html#indenpendence-test-for-ordinal-data"><i class="fa fa-check"></i><b>2.5</b> 有序数据的独立性检验</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#饮酒与婴儿畸形"><i class="fa fa-check"></i>饮酒与婴儿畸形</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="contingency-table.html"><a href="contingency-table.html#exact-test-for-small-sample"><i class="fa fa-check"></i><b>2.6</b> 小样本的精确推断</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#女士品茶"><i class="fa fa-check"></i>女士品茶</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="contingency-table.html"><a href="contingency-table.html#three-way-table"><i class="fa fa-check"></i><b>2.7</b> 三项列联表的关联性</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#死刑判决案例"><i class="fa fa-check"></i>死刑判决案例</a></li>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#临床试验"><i class="fa fa-check"></i>临床试验</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#problems-ch2"><i class="fa fa-check"></i>课后题</a><ul>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#第18题"><i class="fa fa-check"></i>第18题</a></li>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#第22题"><i class="fa fa-check"></i>第22题</a></li>
<li class="chapter" data-level="" data-path="contingency-table.html"><a href="contingency-table.html#第33题"><i class="fa fa-check"></i>第33题</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="glm.html"><a href="glm.html"><i class="fa fa-check"></i><b>3</b> 广义线性模型</a><ul>
<li class="chapter" data-level="3.1" data-path="glm.html"><a href="glm.html#components-of-glm"><i class="fa fa-check"></i><b>3.1</b> 广义线性模型的构成部分</a></li>
<li class="chapter" data-level="3.2" data-path="glm.html"><a href="glm.html#glm-for-binary-data"><i class="fa fa-check"></i><b>3.2</b> 二分数据的广义线性模型</a><ul>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#打鼾与心脏病"><i class="fa fa-check"></i>打鼾与心脏病</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="glm.html"><a href="glm.html#glm-for-count-data"><i class="fa fa-check"></i><b>3.3</b> 计数数据的广义线性模型</a><ul>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#母鲎及其追随者泊松glm"><i class="fa fa-check"></i>母鲎及其追随者（泊松GLM）</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#母鲎及其追随者负二项glm"><i class="fa fa-check"></i>母鲎及其追随者（负二项GLM）</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#英国的火车事故"><i class="fa fa-check"></i>英国的火车事故</a></li>
</ul></li>
<li class="chapter" data-level="3.4" data-path="glm.html"><a href="glm.html#stat-infer-glm"><i class="fa fa-check"></i><b>3.4</b> 统计推断和模型检验</a><ul>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#打鼾与心脏病-1"><i class="fa fa-check"></i>打鼾与心脏病</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="glm.html"><a href="glm.html#fit-glm"><i class="fa fa-check"></i><b>3.5</b> 广义线性模型的拟合</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#problems-ch3"><i class="fa fa-check"></i>课后题</a><ul>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#第3题"><i class="fa fa-check"></i>第3题</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#第4题-1"><i class="fa fa-check"></i>第4题</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#第7题"><i class="fa fa-check"></i>第7题</a></li>
<li class="chapter" data-level="" data-path="glm.html"><a href="glm.html#第20题"><i class="fa fa-check"></i>第20题</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="logistic-regression.html"><a href="logistic-regression.html"><i class="fa fa-check"></i><b>4</b> logistic回归</a><ul>
<li class="chapter" data-level="4.1" data-path="logistic-regression.html"><a href="logistic-regression.html#interpret-logistic"><i class="fa fa-check"></i><b>4.1</b> logistic回归模型的解释</a><ul>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#母鲎及其追随者logistic回归"><i class="fa fa-check"></i>母鲎及其追随者（logistic回归）</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="logistic-regression.html"><a href="logistic-regression.html#infer-logistic"><i class="fa fa-check"></i><b>4.2</b> logistic回归的推断</a></li>
<li class="chapter" data-level="4.3" data-path="logistic-regression.html"><a href="logistic-regression.html#cate-var-logistic"><i class="fa fa-check"></i><b>4.3</b> 属性预测变量的logistic回归</a><ul>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#azt和aids"><i class="fa fa-check"></i>AZT和AIDS</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="logistic-regression.html"><a href="logistic-regression.html#multi-logistic"><i class="fa fa-check"></i><b>4.4</b> 多元logistic回归</a><ul>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#母鲎及其追随者多元logistic"><i class="fa fa-check"></i>母鲎及其追随者（多元logistic）</a></li>
</ul></li>
<li class="chapter" data-level="4.5" data-path="logistic-regression.html"><a href="logistic-regression.html#logistic回归效应的概括"><i class="fa fa-check"></i><b>4.5</b> logistic回归效应的概括</a></li>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#problem-ch4"><i class="fa fa-check"></i>课后题</a><ul>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#第8题"><i class="fa fa-check"></i>第8题</a></li>
<li class="chapter" data-level="" data-path="logistic-regression.html"><a href="logistic-regression.html#第24题"><i class="fa fa-check"></i>第24题</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html"><i class="fa fa-check"></i><b>5</b> logistic回归模型的构建和应用</a><ul>
<li class="chapter" data-level="5.1" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#model-selection"><i class="fa fa-check"></i><b>5.1</b> 模型选择策略</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#母鲎及其追随者模型选择"><i class="fa fa-check"></i>母鲎及其追随者（模型选择）</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#母鲎及其追随者预测功效"><i class="fa fa-check"></i>母鲎及其追随者（预测功效）</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#model-checking"><i class="fa fa-check"></i><b>5.2</b> 模型检验</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#母鲎及其追随者模型lr检验"><i class="fa fa-check"></i>母鲎及其追随者（模型LR检验）</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#azt和aids拟合优度"><i class="fa fa-check"></i>AZT和AIDS（拟合优度）</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#母鲎及其追随者hm检验"><i class="fa fa-check"></i>母鲎及其追随者（HM检验）</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#佛罗里达大学研究生入学"><i class="fa fa-check"></i>佛罗里达大学研究生入学</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#心脏病与血压的关系"><i class="fa fa-check"></i>心脏病与血压的关系</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#sparse-data-logistic"><i class="fa fa-check"></i><b>5.3</b> 稀疏数据效应</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#稀疏数据的临床试验结果"><i class="fa fa-check"></i>稀疏数据的临床试验结果</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#conditional-logistic"><i class="fa fa-check"></i><b>5.4</b> 条件logistic回归与精确推断</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#晋升能力"><i class="fa fa-check"></i>晋升能力</a></li>
</ul></li>
<li class="chapter" data-level="5.5" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#logistic-sample-num"><i class="fa fa-check"></i><b>5.5</b> logistic回归的样本量与功效</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#样本量计算"><i class="fa fa-check"></i>样本量计算</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#problem-ch5"><i class="fa fa-check"></i>课后题</a><ul>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#第10题"><i class="fa fa-check"></i>第10题</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#第18题-1"><i class="fa fa-check"></i>第18题</a></li>
<li class="chapter" data-level="" data-path="build-and-apply-logistic-model.html"><a href="build-and-apply-logistic-model.html#第28题"><i class="fa fa-check"></i>第28题</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="6" data-path="multi-logit-model.html"><a href="multi-logit-model.html"><i class="fa fa-check"></i><b>6</b> 多类别logit模型</a><ul>
<li class="chapter" data-level="6.1" data-path="multi-logit-model.html"><a href="multi-logit-model.html#nomial-logit"><i class="fa fa-check"></i><b>6.1</b> 名义响应变量的logit模型</a><ul>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#钝吻鳄食物选择"><i class="fa fa-check"></i>钝吻鳄食物选择</a></li>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#是否相信来世"><i class="fa fa-check"></i>是否相信来世</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="multi-logit-model.html"><a href="multi-logit-model.html#ordinal-logit"><i class="fa fa-check"></i><b>6.2</b> 有序响应变量的累积logit模型</a><ul>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#政治意识形态和隶属党派的关系"><i class="fa fa-check"></i>政治意识形态和隶属党派的关系</a></li>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#对心理健康建模"><i class="fa fa-check"></i>对心理健康建模</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="multi-logit-model.html"><a href="multi-logit-model.html#paired-ordinal-logit"><i class="fa fa-check"></i><b>6.3</b> 成对类别有序logit</a><ul>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#再访政治意识形态"><i class="fa fa-check"></i>再访政治意识形态</a></li>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#发育毒性研究"><i class="fa fa-check"></i>发育毒性研究</a></li>
</ul></li>
<li class="chapter" data-level="6.4" data-path="multi-logit-model.html"><a href="multi-logit-model.html#conditional-independent"><i class="fa fa-check"></i><b>6.4</b> 条件独立性检验</a><ul>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#工作满意度和收入"><i class="fa fa-check"></i>工作满意度和收入</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="multi-logit-model.html"><a href="multi-logit-model.html#ch6-problems"><i class="fa fa-check"></i>课后题</a></li>
</ul></li>
<li class="appendix"><span><b>附录</b></span></li>
<li class="chapter" data-level="A" data-path="r-pkg-intro.html"><a href="r-pkg-intro.html"><i class="fa fa-check"></i><b>A</b> 配套R包使用介绍</a><ul>
<li class="chapter" data-level="A.1" data-path="r-pkg-intro.html"><a href="r-pkg-intro.html#r-pkg-install"><i class="fa fa-check"></i><b>A.1</b> 安装</a></li>
<li class="chapter" data-level="A.2" data-path="r-pkg-intro.html"><a href="r-pkg-intro.html#r-pkg-use"><i class="fa fa-check"></i><b>A.2</b> 使用说明</a></li>
</ul></li>
<li class="chapter" data-level="B" data-path="book-dataset-list.html"><a href="book-dataset-list.html"><i class="fa fa-check"></i><b>B</b> 教材数据列表</a><ul>
<li class="chapter" data-level="B.1" data-path="book-dataset-list.html"><a href="book-dataset-list.html#正文案例数据"><i class="fa fa-check"></i><b>B.1</b> 正文案例数据</a></li>
<li class="chapter" data-level="B.2" data-path="book-dataset-list.html"><a href="book-dataset-list.html#习题数据"><i class="fa fa-check"></i><b>B.2</b> 习题数据</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://bookdown.org" target="blank">本书由 bookdown 强力驱动</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">《属性数据分析》代码</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="contingency-table" class="section level1">
<h1><span class="header-section-number">第 2 章</span> 列联表</h1>
<div id="stucture" class="section level2">
<h2><span class="header-section-number">2.1</span> 列联表的概率结构</h2>
<div id="关于来世" class="section level3 unnumbered">
<h3>关于来世</h3>
<div class="sourceCode" id="cb55"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb55-1" data-line-number="1"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb55-2" data-line-number="2"><span class="kw">data</span>(<span class="st">&quot;afterlife1&quot;</span>)</a>
<a class="sourceLine" id="cb55-3" data-line-number="3">afterlife1</a></code></pre></div>
<pre><code>##          Belief
## Gender    Yes No or Undecided
##   Females 509             116
##   Males   398             104</code></pre>
<div class="sourceCode" id="cb57"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb57-1" data-line-number="1"><span class="kw">margin.table</span>(afterlife1, <span class="dt">margin =</span> <span class="dv">1</span>)  <span class="co"># 求行和</span></a></code></pre></div>
<pre><code>## Gender
## Females   Males 
##     625     502</code></pre>
<div class="sourceCode" id="cb59"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb59-1" data-line-number="1"><span class="kw">margin.table</span>(afterlife1, <span class="dt">margin =</span> <span class="dv">2</span>)  <span class="co"># 求列和</span></a></code></pre></div>
<pre><code>## Belief
##             Yes No or Undecided 
##             907             220</code></pre>
<div class="sourceCode" id="cb61"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb61-1" data-line-number="1"><span class="kw">addmargins</span>(afterlife1)  <span class="co"># 将行列求和加入列联表</span></a></code></pre></div>
<pre><code>##          Belief
## Gender     Yes No or Undecided  Sum
##   Females  509             116  625
##   Males    398             104  502
##   Sum      907             220 1127</code></pre>
<div class="sourceCode" id="cb63"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb63-1" data-line-number="1"><span class="kw">prop.table</span>(afterlife1, <span class="dt">margin =</span> <span class="dv">1</span>)  <span class="co"># 求给定行的条件分布</span></a></code></pre></div>
<pre><code>##          Belief
## Gender       Yes No or Undecided
##   Females 0.8144          0.1856
##   Males   0.7928          0.2072</code></pre>
<div class="sourceCode" id="cb65"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb65-1" data-line-number="1"><span class="kw">prop.table</span>(afterlife1, <span class="dt">margin =</span> <span class="dv">2</span>)  <span class="co"># 求给定列的条件分布</span></a></code></pre></div>
<pre><code>##          Belief
## Gender       Yes No or Undecided
##   Females 0.5612          0.5273
##   Males   0.4388          0.4727</code></pre>
</div>
</div>
<div id="prop-compare" class="section level2">
<h2><span class="header-section-number">2.2</span> 2×2表比例的比较</h2>
<div id="阿司匹林与心脏病列联表检验" class="section level3 unnumbered">
<h3>阿司匹林与心脏病（列联表检验）</h3>
<div class="sourceCode" id="cb67"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb67-1" data-line-number="1"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb67-2" data-line-number="2"><span class="kw">data</span>(<span class="st">&quot;aspirin&quot;</span>)</a>
<a class="sourceLine" id="cb67-3" data-line-number="3">aspirin</a></code></pre></div>
<pre><code>##          MI
## Group         Y     N
##   Placebo   189 10845
##   Aspirin   104 10933</code></pre>
<div class="sourceCode" id="cb69"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb69-1" data-line-number="1"><span class="kw">margin.table</span>(aspirin, <span class="dv">1</span>)  <span class="co"># 服用安慰剂和阿司匹林的人数</span></a></code></pre></div>
<pre><code>## Group
## Placebo Aspirin 
##   11034   11037</code></pre>
<div class="sourceCode" id="cb71"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb71-1" data-line-number="1"><span class="kw">prop.table</span>(aspirin, <span class="dv">1</span>)  <span class="co"># 两个组中患心肌梗死的比例</span></a></code></pre></div>
<pre><code>##          MI
## Group            Y        N
##   Placebo 0.017129 0.982871
##   Aspirin 0.009423 0.990577</code></pre>
<div class="sourceCode" id="cb73"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb73-1" data-line-number="1"><span class="kw">prop.test</span>(aspirin)  <span class="co"># 对两个患病比率是否相同进行检验并求出置信区间</span></a></code></pre></div>
<pre><code>## 
##  2-sample test for equality of proportions with
##  continuity correction
## 
## data:  aspirin
## X-squared = 24, df = 1, p-value = 8e-07
## alternative hypothesis: two.sided
## 95 percent confidence interval:
##  0.004597 0.010815
## sample estimates:
##   prop 1   prop 2 
## 0.017129 0.009423</code></pre>
</div>
</div>
<div id="odds-ratio" class="section level2">
<h2><span class="header-section-number">2.3</span> 优势比</h2>
<div id="阿司匹林与心脏病优势比" class="section level3 unnumbered">
<h3>阿司匹林与心脏病（优势比）</h3>
<p>计算优势比可使用本文档配套包<code>cdabookcode</code>里的<code>oddsratio</code>计算，使用详情请<code>?oddsratio</code></p>
<div class="sourceCode" id="cb75"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb75-1" data-line-number="1"><span class="kw">library</span>(cdabookfunc)</a>
<a class="sourceLine" id="cb75-2" data-line-number="2"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb75-3" data-line-number="3"><span class="kw">data</span>(<span class="st">&quot;aspirin&quot;</span>)</a>
<a class="sourceLine" id="cb75-4" data-line-number="4"><span class="kw">oddsratio</span>(aspirin)  <span class="co"># 优势比</span></a></code></pre></div>
<pre><code>## [1] 1.832</code></pre>
</div>
<div id="吸烟状态与心肌梗死" class="section level3 unnumbered">
<h3>吸烟状态与心肌梗死</h3>
<div class="sourceCode" id="cb77"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb77-1" data-line-number="1"><span class="kw">library</span>(cdabookfunc)</a>
<a class="sourceLine" id="cb77-2" data-line-number="2"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb77-3" data-line-number="3"><span class="kw">data</span>(<span class="st">&quot;smoking_mi&quot;</span>)</a>
<a class="sourceLine" id="cb77-4" data-line-number="4"><span class="kw">oddsratio</span>(smoking_mi, <span class="dt">row_id =</span> <span class="kw">c</span>(<span class="dv">2</span>, <span class="dv">1</span>))  <span class="co"># 优势比</span></a></code></pre></div>
<pre><code>## [1] 3.822</code></pre>
</div>
</div>
<div id="chi-square-test" class="section level2">
<h2><span class="header-section-number">2.4</span> 独立性的卡方检验</h2>
<div id="性别和党派认同" class="section level3 unnumbered">
<h3>性别和党派认同</h3>
<div class="sourceCode" id="cb79"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb79-1" data-line-number="1"><span class="kw">library</span>(cdabookfunc)</a>
<a class="sourceLine" id="cb79-2" data-line-number="2"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb79-3" data-line-number="3"><span class="kw">data</span>(<span class="st">&quot;gender_party&quot;</span>)</a>
<a class="sourceLine" id="cb79-4" data-line-number="4"><span class="kw">oddsratio</span>(gender_party, <span class="dt">col_id =</span> <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">3</span>))  <span class="co"># 优势比</span></a></code></pre></div>
<pre><code>## [1] 1.605</code></pre>
<p>卡方检验可直接使用<code>chisq.test()</code>完成</p>
<div class="sourceCode" id="cb81"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb81-1" data-line-number="1"><span class="co"># X2 test</span></a>
<a class="sourceLine" id="cb81-2" data-line-number="2">x2_result &lt;-<span class="st"> </span><span class="kw">chisq.test</span>(gender_party)  <span class="co"># 独立性卡方检验</span></a>
<a class="sourceLine" id="cb81-3" data-line-number="3">x2_result</a></code></pre></div>
<pre><code>## 
##  Pearson&#39;s Chi-squared test
## 
## data:  gender_party
## X-squared = 30, df = 2, p-value = 3e-07</code></pre>
<p>G2统计量的计算需要先得到独立性假设下的期望值，也可从<code>chisq.test()</code>的结果中得到</p>
<div class="sourceCode" id="cb83"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb83-1" data-line-number="1"><span class="co"># G2</span></a>
<a class="sourceLine" id="cb83-2" data-line-number="2">gender_party_expected &lt;-<span class="st"> </span>x2_result<span class="op">$</span>expected  <span class="co"># 获取独立性假设下的期望值</span></a>
<a class="sourceLine" id="cb83-3" data-line-number="3">gender_party_expected</a></code></pre></div>
<pre><code>##          Party
## Gender    Democrat Independent Republican
##   Females    703.7       319.6      533.7
##   Males      542.3       246.4      411.3</code></pre>
<div class="sourceCode" id="cb85"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb85-1" data-line-number="1">Gsq &lt;-<span class="st"> </span><span class="dv">2</span> <span class="op">*</span><span class="st"> </span><span class="kw">sum</span>(gender_party <span class="op">*</span><span class="st"> </span><span class="kw">log</span>(gender_party <span class="op">/</span><span class="st"> </span>gender_party_expected))</a>
<a class="sourceLine" id="cb85-2" data-line-number="2">pvalue &lt;-<span class="st"> </span><span class="dv">1</span> <span class="op">-</span><span class="st"> </span><span class="kw">pchisq</span>(Gsq, <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb85-3" data-line-number="3">Gsq; pvalue</a></code></pre></div>
<pre><code>## [1] 30.02</code></pre>
<pre><code>## [1] 3.034e-07</code></pre>
<p>此外，X2和G2检验也可以使用<code>cdabookcode</code>中的<code>independent_test_of_table()</code>实现</p>
<div class="sourceCode" id="cb88"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb88-1" data-line-number="1"><span class="kw">independent_test_of_table</span>(gender_party, <span class="st">&quot;X2&quot;</span>)</a></code></pre></div>
<pre><code>## $method
## [1] &quot;X2&quot;
## 
## $statistic
## [1] 30.07
## 
## $df
## [1] 2
## 
## $p.value
## [1] 2.954e-07</code></pre>
<div class="sourceCode" id="cb90"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb90-1" data-line-number="1"><span class="kw">independent_test_of_table</span>(gender_party, <span class="st">&quot;G2&quot;</span>)</a></code></pre></div>
<pre><code>## $method
## [1] &quot;G2&quot;
## 
## $statistic
## [1] 30.02
## 
## $df
## [1] 2
## 
## $p.value
## [1] 3.034e-07</code></pre>
<p>残差和标准化残差同样可以从<code>chisq.test()</code>的结果中得到</p>
<div class="sourceCode" id="cb92"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb92-1" data-line-number="1"><span class="co"># 残差</span></a>
<a class="sourceLine" id="cb92-2" data-line-number="2">gender_party <span class="op">-</span><span class="st"> </span>gender_party_expected</a></code></pre></div>
<pre><code>##          Party
## Gender    Democrat Independent Republican
##   Females   58.329       7.355    -65.683
##   Males    -58.329      -7.355     65.683</code></pre>
<div class="sourceCode" id="cb94"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb94-1" data-line-number="1"><span class="co"># 标准化残差</span></a>
<a class="sourceLine" id="cb94-2" data-line-number="2">x2_result<span class="op">$</span>stdres</a></code></pre></div>
<pre><code>##          Party
## Gender    Democrat Independent Republican
##   Females   4.5021      0.6995    -5.3159
##   Males    -4.5021     -0.6995     5.3159</code></pre>
</div>
</div>
<div id="indenpendence-test-for-ordinal-data" class="section level2">
<h2><span class="header-section-number">2.5</span> 有序数据的独立性检验</h2>
<div id="饮酒与婴儿畸形" class="section level3 unnumbered">
<h3>饮酒与婴儿畸形</h3>
<p>M2检验也可以使用<code>independent_test_of_table()</code>实现</p>
<div class="sourceCode" id="cb96"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb96-1" data-line-number="1"><span class="kw">library</span>(cdabookfunc)</a>
<a class="sourceLine" id="cb96-2" data-line-number="2"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb96-3" data-line-number="3"><span class="kw">data</span>(<span class="st">&quot;malformation&quot;</span>)</a>
<a class="sourceLine" id="cb96-4" data-line-number="4"><span class="co"># 对比X2, G2, M2的结果</span></a>
<a class="sourceLine" id="cb96-5" data-line-number="5"><span class="co"># 使用method=&quot;all&quot;可以同时进行X2, G2, M2检验</span></a>
<a class="sourceLine" id="cb96-6" data-line-number="6"><span class="kw">independent_test_of_table</span>(malformation, <span class="st">&quot;all&quot;</span>, <span class="kw">c</span>(<span class="dv">0</span>, <span class="fl">0.5</span>, <span class="fl">1.5</span>, <span class="dv">4</span>, <span class="dv">7</span>), <span class="dv">0</span><span class="op">:</span><span class="dv">1</span>)</a></code></pre></div>
<pre><code>## Warning in chisq.test(x): Chi-squared approximation may be
## incorrect</code></pre>
<pre><code>##      method statistic df p.value
## [1,] &quot;X2&quot;   12.08     4  0.01675
## [2,] &quot;G2&quot;   6.202     4  0.1846 
## [3,] &quot;M2&quot;   6.57      1  0.01037</code></pre>
<p>u和v的选取会影响结果</p>
<div class="sourceCode" id="cb99"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb99-1" data-line-number="1"><span class="kw">independent_test_of_table</span>(malformation, <span class="st">&quot;G2&quot;</span>, <span class="dv">1</span><span class="op">:</span><span class="dv">5</span>, <span class="dv">0</span><span class="op">:</span><span class="dv">1</span>)</a></code></pre></div>
<pre><code>## $method
## [1] &quot;G2&quot;
## 
## $statistic
## [1] 6.202
## 
## $df
## [1] 4
## 
## $p.value
## [1] 0.1846</code></pre>
</div>
</div>
<div id="exact-test-for-small-sample" class="section level2">
<h2><span class="header-section-number">2.6</span> 小样本的精确推断</h2>
<div id="女士品茶" class="section level3 unnumbered">
<h3>女士品茶</h3>
<div class="sourceCode" id="cb101"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb101-1" data-line-number="1"><span class="co"># 计算概率（超几何分布）</span></a>
<a class="sourceLine" id="cb101-2" data-line-number="2"><span class="kw">dhyper</span>(<span class="dv">0</span><span class="op">:</span><span class="dv">4</span>, <span class="dv">4</span>, <span class="dv">4</span>, <span class="dv">4</span>)</a></code></pre></div>
<pre><code>## [1] 0.01429 0.22857 0.51429 0.22857 0.01429</code></pre>
<p>费雪精确检验可使用<code>fisher.test()</code></p>
<div class="sourceCode" id="cb103"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb103-1" data-line-number="1">tea_tasting &lt;-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">c</span>(<span class="dv">3</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">3</span>), <span class="dt">nrow =</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb103-2" data-line-number="2"><span class="kw">fisher.test</span>(tea_tasting, <span class="dt">alternative =</span> <span class="st">&quot;g&quot;</span>)</a></code></pre></div>
<pre><code>## 
##  Fisher&#39;s Exact Test for Count Data
## 
## data:  tea_tasting
## p-value = 0.2
## alternative hypothesis: true odds ratio is greater than 1
## 95 percent confidence interval:
##  0.3136    Inf
## sample estimates:
## odds ratio 
##      6.408</code></pre>
<div class="sourceCode" id="cb105"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb105-1" data-line-number="1"><span class="kw">fisher.test</span>(tea_tasting, <span class="dt">alternative =</span> <span class="st">&quot;t&quot;</span>)</a></code></pre></div>
<pre><code>## 
##  Fisher&#39;s Exact Test for Count Data
## 
## data:  tea_tasting
## p-value = 0.5
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
##    0.2117 621.9338
## sample estimates:
## odds ratio 
##      6.408</code></pre>
</div>
</div>
<div id="three-way-table" class="section level2">
<h2><span class="header-section-number">2.7</span> 三项列联表的关联性</h2>
<div id="死刑判决案例" class="section level3 unnumbered">
<h3>死刑判决案例</h3>
<div class="sourceCode" id="cb107"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb107-1" data-line-number="1"><span class="kw">library</span>(cdabookfunc)</a>
<a class="sourceLine" id="cb107-2" data-line-number="2"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb107-3" data-line-number="3"><span class="kw">data</span>(<span class="st">&quot;deathpenalty1&quot;</span>)</a>
<a class="sourceLine" id="cb107-4" data-line-number="4"><span class="kw">ftable</span>(deathpenalty1)</a></code></pre></div>
<pre><code>##                  DeathPenalty Yes  No
## Defendant Victim                     
## White     White                53 414
##           Black                 0  16
## Black     White                11  37
##           Black                 4 139</code></pre>
<div class="sourceCode" id="cb109"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb109-1" data-line-number="1"><span class="co"># 被判死刑的比例</span></a>
<a class="sourceLine" id="cb109-2" data-line-number="2"><span class="kw">prop.table</span>(deathpenalty1, <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>))[, , <span class="dv">1</span>]</a></code></pre></div>
<pre><code>##          Victim
## Defendant   White   Black
##     White 0.11349 0.00000
##     Black 0.22917 0.02797</code></pre>
<div class="sourceCode" id="cb111"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb111-1" data-line-number="1"><span class="co"># 根据被告种族，被判死刑的比例</span></a>
<a class="sourceLine" id="cb111-2" data-line-number="2"><span class="kw">prop.table</span>(<span class="kw">margin.table</span>(deathpenalty1, <span class="dt">margin =</span> <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">3</span>)), <span class="dt">margin =</span> <span class="dv">1</span>)[, <span class="dv">1</span>]</a></code></pre></div>
<pre><code>##   White   Black 
## 0.10973 0.07853</code></pre>
<div class="sourceCode" id="cb113"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb113-1" data-line-number="1"><span class="co"># 受害者为白人时的优势比（条件优势比）</span></a>
<a class="sourceLine" id="cb113-2" data-line-number="2"><span class="kw">oddsratio</span>(deathpenalty1[, <span class="dv">1</span>, ])</a></code></pre></div>
<pre><code>## [1] 0.4306</code></pre>
<div class="sourceCode" id="cb115"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb115-1" data-line-number="1"><span class="co"># 不考虑受害者时的优势比（边际优势比）</span></a>
<a class="sourceLine" id="cb115-2" data-line-number="2"><span class="kw">oddsratio</span>(<span class="kw">margin.table</span>(deathpenalty1, <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">3</span>)))</a></code></pre></div>
<pre><code>## [1] 1.446</code></pre>
</div>
<div id="临床试验" class="section level3 unnumbered">
<h3>临床试验</h3>
<div class="sourceCode" id="cb117"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb117-1" data-line-number="1"><span class="kw">library</span>(cdabookfunc)</a>
<a class="sourceLine" id="cb117-2" data-line-number="2"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb117-3" data-line-number="3"><span class="kw">data</span>(<span class="st">&quot;treatment1&quot;</span>)</a>
<a class="sourceLine" id="cb117-4" data-line-number="4"></a>
<a class="sourceLine" id="cb117-5" data-line-number="5"><span class="co"># 条件优势比（clinic=1）</span></a>
<a class="sourceLine" id="cb117-6" data-line-number="6"><span class="kw">oddsratio</span>(treatment1[<span class="dv">1</span>, ,])</a></code></pre></div>
<pre><code>## [1] 1</code></pre>
<div class="sourceCode" id="cb119"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb119-1" data-line-number="1"><span class="co"># 条件优势比（clinic=1）</span></a>
<a class="sourceLine" id="cb119-2" data-line-number="2"><span class="kw">oddsratio</span>(<span class="kw">margin.table</span>(treatment1, <span class="kw">c</span>(<span class="dv">2</span>, <span class="dv">3</span>)))</a></code></pre></div>
<pre><code>## [1] 2</code></pre>
</div>
</div>
<div id="problems-ch2" class="section level2 unnumbered">
<h2>课后题</h2>
<div id="第18题" class="section level3 unnumbered">
<h3>第18题</h3>
<ol style="list-style-type: lower-alpha">
<li></li>
</ol>
<div class="sourceCode" id="cb121"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb121-1" data-line-number="1"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb121-2" data-line-number="2"><span class="kw">data</span>(<span class="st">&quot;happiness1&quot;</span>)</a>
<a class="sourceLine" id="cb121-3" data-line-number="3">happiness1</a></code></pre></div>
<pre><code>##           Happiness
## Income     NotTooHappy PrettyHappy VeryHappy
##   AboveAvg          21         159       110
##   Avg               53         372       221
##   BelowAvg          94         249        83</code></pre>
<p>The formula to calculate the estimated expected cell count is <span class="math inline">\(\hat{\mu}_{11} = \frac{n_{1+}n_{+1}}{n}\)</span></p>
<div class="sourceCode" id="cb123"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb123-1" data-line-number="1">mu &lt;-<span class="st"> </span><span class="kw">rowSums</span>(happiness1)[<span class="dv">1</span>] <span class="op">*</span><span class="st"> </span><span class="kw">colSums</span>(happiness1)[<span class="dv">1</span>] <span class="op">/</span><span class="st"> </span><span class="kw">sum</span>(happiness1)</a>
<a class="sourceLine" id="cb123-2" data-line-number="2"><span class="kw">unname</span>(mu)</a></code></pre></div>
<pre><code>## [1] 35.77</code></pre>
<p>Then we get that <span class="math inline">\(\hat{\mu}_{11} = 35.8\)</span></p>
<ol start="2" style="list-style-type: lower-alpha">
<li>The formula to calculate df is <span class="math inline">\(df=(I-1)(J-1)\)</span></li>
</ol>
<div class="sourceCode" id="cb125"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb125-1" data-line-number="1">df &lt;-<span class="st"> </span><span class="kw">prod</span>(<span class="kw">dim</span>(happiness1) <span class="op">-</span><span class="st"> </span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb125-2" data-line-number="2">Pv &lt;-<span class="st"> </span><span class="dv">1</span> <span class="op">-</span><span class="st"> </span><span class="kw">pchisq</span>(<span class="fl">73.4</span>, df)</a>
<a class="sourceLine" id="cb125-3" data-line-number="3">df; Pv</a></code></pre></div>
<pre><code>## [1] 4</code></pre>
<pre><code>## [1] 4.33e-15</code></pre>
<p>Then we get that <span class="math inline">\(df=4\)</span>, <span class="math inline">\(pvalue = 0\)</span>.</p>
<ol start="3" style="list-style-type: lower-alpha">
<li><p>These show a greater discrepancy between <span class="math inline">\(n_{11}\)</span> and <span class="math inline">\(\hat{\mu}_{11}\)</span> (<span class="math inline">\(n_{33}\)</span> and<span class="math inline">\(\hat{\mu}_{33}\)</span> ) than we would expect if the
variables were truly independent. There have large negative residuals for above average income not very
happy person and below average income very happy person. Thus, there were fewer people than the
hypothesis of independence predicts.</p></li>
<li><p>There have large positive residuals for above average income very happy person and below average income
not very happy person. Thus, there were more people than the hypothesis of independence predicts.</p></li>
</ol>
</div>
<div id="第22题" class="section level3 unnumbered">
<h3>第22题</h3>
<ol style="list-style-type: lower-alpha">
<li></li>
</ol>
<div class="sourceCode" id="cb128"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb128-1" data-line-number="1"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb128-2" data-line-number="2"><span class="kw">data</span>(<span class="st">&quot;psych_diag_drugs&quot;</span>)</a>
<a class="sourceLine" id="cb128-3" data-line-number="3">psych_diag_drugs</a></code></pre></div>
<pre><code>##                      Drugs
## Diagnosis               Y   N
##   Schizophrenia       105   8
##   AffectiveDisorder    12   2
##   Neurosis             18  19
##   PersonalityDisorder  47  52
##   SpecialSymptoms       0  13</code></pre>
<p><span class="math display">\[\hat{\mu}_{11} = \frac{n_{1+}n_{+1}}{n}\]</span></p>
<p><span class="math display">\[SR = \frac{n_{ij}-\hat{\mu}_{ij}}{\sqrt{\hat{\mu}_{ij}(1-p_{i+})(1-p_{+j})}}\]</span></p>
<p><span class="math display">\[X^2 = \sum \frac{(n_{ij}-\hat{\mu}_{ij})^2}{\hat{\mu}_{ij}}\]</span></p>
<div class="sourceCode" id="cb130"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb130-1" data-line-number="1"><span class="co"># X-squared, df and p-value</span></a>
<a class="sourceLine" id="cb130-2" data-line-number="2"><span class="kw">chisq.test</span>(psych_diag_drugs)</a></code></pre></div>
<pre><code>## Warning in chisq.test(psych_diag_drugs): Chi-squared
## approximation may be incorrect</code></pre>
<pre><code>## 
##  Pearson&#39;s Chi-squared test
## 
## data:  psych_diag_drugs
## X-squared = 84, df = 4, p-value &lt;2e-16</code></pre>
<div class="sourceCode" id="cb133"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb133-1" data-line-number="1"><span class="co"># standard residual</span></a>
<a class="sourceLine" id="cb133-2" data-line-number="2"><span class="kw">chisq.test</span>(psych_diag_drugs)<span class="op">$</span>stdres</a></code></pre></div>
<pre><code>## Warning in chisq.test(psych_diag_drugs): Chi-squared
## approximation may be incorrect</code></pre>
<pre><code>##                      Drugs
## Diagnosis                  Y      N
##   Schizophrenia        7.875 -7.875
##   AffectiveDisorder    1.602 -1.602
##   Neurosis            -2.385  2.385
##   PersonalityDisorder -4.842  4.842
##   SpecialSymptoms     -5.139  5.139</code></pre>
<p>We could obtain <span class="math inline">\(X^2 = 84.188, df=4\)</span>,and P-value almost equals to 0.Thus, we should reject null hypothesis, which means
psychiatric diagnosis and whether patients have drugs are not independent. Positive standardized residuals
means there are more people than expected and negative standardized residuals means there are less
people than expected. If the absolute value of standardized residuals less than 2, then we do not have strong
evidence to reject the null hypothesis.</p>
<ol start="3" style="list-style-type: lower-alpha">
<li></li>
</ol>
<p>i). the first two rows</p>
<div class="sourceCode" id="cb136"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb136-1" data-line-number="1">psy12 &lt;-<span class="st"> </span>psych_diag_drugs[<span class="dv">1</span><span class="op">:</span><span class="dv">2</span>,] </a>
<a class="sourceLine" id="cb136-2" data-line-number="2"><span class="kw">chisq.test</span>(psy12)</a></code></pre></div>
<pre><code>## Warning in chisq.test(psy12): Chi-squared approximation may
## be incorrect</code></pre>
<pre><code>## 
##  Pearson&#39;s Chi-squared test with Yates&#39; continuity
##  correction
## 
## data:  psy12
## X-squared = 0.17, df = 1, p-value = 0.7</code></pre>
<p>Then we get <span class="math inline">\(X^2 = 0.175\)</span>, df=1, and P-value = 0.6757,then we can not reject null hypothesis.</p>
<p>ii). the third and fourth rows</p>
<div class="sourceCode" id="cb139"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb139-1" data-line-number="1">psy34 &lt;-<span class="st"> </span>psych_diag_drugs[<span class="dv">3</span><span class="op">:</span><span class="dv">4</span>,]</a>
<a class="sourceLine" id="cb139-2" data-line-number="2"><span class="kw">chisq.test</span>(psy34)</a></code></pre></div>
<pre><code>## 
##  Pearson&#39;s Chi-squared test with Yates&#39; continuity
##  correction
## 
## data:  psy34
## X-squared = 1.4e-30, df = 1, p-value = 1</code></pre>
<p>Then we get <span class="math inline">\(X^2\)</span> almost equals to 0 , df=1, and P-value = 1,then we can not reject null hypothesis.</p>
<p>iii). the last row to the first and second rows combined and the third and fourth rows combined</p>
<div class="sourceCode" id="cb141"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb141-1" data-line-number="1">psy0 &lt;-<span class="st"> </span><span class="kw">rbind</span>(<span class="kw">colSums</span>(psy12), <span class="kw">colSums</span>(psy34), psych_diag_drugs[<span class="dv">5</span>, ])</a>
<a class="sourceLine" id="cb141-2" data-line-number="2"><span class="kw">chisq.test</span>(psy0)</a></code></pre></div>
<pre><code>## Warning in chisq.test(psy0): Chi-squared approximation may
## be incorrect</code></pre>
<pre><code>## 
##  Pearson&#39;s Chi-squared test
## 
## data:  psy0
## X-squared = 84, df = 2, p-value &lt;2e-16</code></pre>
<p>Then we get <span class="math inline">\(X^2 = 83.884\)</span> , df=2, and P-value almost equals to 0 ,then we can reject null hypothesis, psychiatric diagnosis and whether
patients have drugs are not independent.</p>
</div>
<div id="第33题" class="section level3 unnumbered">
<h3>第33题</h3>
<ol style="list-style-type: lower-alpha">
<li></li>
</ol>
<div class="sourceCode" id="cb144"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb144-1" data-line-number="1"><span class="kw">library</span>(cdabookfunc)</a>
<a class="sourceLine" id="cb144-2" data-line-number="2"><span class="kw">library</span>(cdabookdb)</a>
<a class="sourceLine" id="cb144-3" data-line-number="3"><span class="kw">data</span>(<span class="st">&quot;deathpenalty2&quot;</span>)</a>
<a class="sourceLine" id="cb144-4" data-line-number="4"><span class="kw">ftable</span>(deathpenalty2)</a></code></pre></div>
<pre><code>##                  DeathPenalty Yes  No
## Defendant Victim                     
## White     White                19 132
##           Black                 0   9
## Black     White                11  52
##           Black                 6  97</code></pre>
<ol start="2" style="list-style-type: lower-alpha">
<li></li>
</ol>
<div class="sourceCode" id="cb146"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb146-1" data-line-number="1"><span class="co"># When victim is white</span></a>
<a class="sourceLine" id="cb146-2" data-line-number="2">deathpenalty2[, <span class="dv">1</span>, ]</a></code></pre></div>
<pre><code>##          DeathPenalty
## Defendant Yes  No
##     White  19 132
##     Black  11  52</code></pre>
<div class="sourceCode" id="cb148"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb148-1" data-line-number="1"><span class="kw">oddsratio</span>(deathpenalty2[, <span class="dv">1</span>, ], <span class="fl">0.5</span>)</a></code></pre></div>
<pre><code>## [1] 0.6719</code></pre>
<div class="sourceCode" id="cb150"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb150-1" data-line-number="1"><span class="co"># When victim is black</span></a>
<a class="sourceLine" id="cb150-2" data-line-number="2">deathpenalty2[, <span class="dv">2</span>, ]</a></code></pre></div>
<pre><code>##          DeathPenalty
## Defendant Yes No
##     White   0  9
##     Black   6 97</code></pre>
<div class="sourceCode" id="cb152"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb152-1" data-line-number="1"><span class="kw">oddsratio</span>(deathpenalty2[, <span class="dv">2</span>, ], <span class="fl">0.5</span>)</a></code></pre></div>
<pre><code>## [1] 0.7895</code></pre>
<p>Controlling for victims’ race, the percentage of “yes” death penalty verdicts was higher for black defendants than for white defendants.</p>
<ol start="3" style="list-style-type: lower-alpha">
<li></li>
</ol>
<div class="sourceCode" id="cb154"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb154-1" data-line-number="1"><span class="co"># Ignorevictims&#39; race</span></a>
<a class="sourceLine" id="cb154-2" data-line-number="2"><span class="kw">margin.table</span>(deathpenalty2, <span class="dv">2</span>)</a></code></pre></div>
<pre><code>## Victim
## White Black 
##   214   112</code></pre>
<div class="sourceCode" id="cb156"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb156-1" data-line-number="1"><span class="kw">oddsratio</span>(<span class="kw">margin.table</span>(deathpenalty2, <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">3</span>)))</a></code></pre></div>
<pre><code>## [1] 1.181</code></pre>
<p>Ignoring for victims’ race, the percentage of “yes” death penalty verdicts was higher for black defendants than for white defendants.</p>
<p>Then these data exhibit Simpson’s paradox.</p>

</div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="intro.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="glm.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook/js/app.min.js"></script>
<script src="libs/gitbook/js/lunr.js"></script>
<script src="libs/gitbook/js/plugin-search.js"></script>
<script src="libs/gitbook/js/plugin-sharing.js"></script>
<script src="libs/gitbook/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook/js/plugin-bookdown.js"></script>
<script src="libs/gitbook/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": false,
"google": false,
"linkedin": false,
"weibo": false,
"instapper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/jinzhen-lin/cdacode-document/edit/master/02-contingency-table.Rmd",
"text": "编辑"
},
"download": ["cdacode.pdf", "cdacode.epub", "cdacode.zip"],
"toc": {
"collapse": "section"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://cdn.bootcss.com/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:" && /^https?:/.test(src))
      src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
